<%@page contentType="text/html;charset=UTF-8"%>
<%@page pageEncoding="UTF-8"%>
<%@ include file="/pub/includ.jsp"%>
<%
	String codeId = request.getParameter("codeId");
%>
<html>
	<head>
		<script type="text/javascript">
		
			//对第一个frm的回调函数, 方法的命名规则为callback_+表单id	
			var callback_frm = function(rows, frm) {
		       $.each(rows, function(i, n){
		    	   var row = $("#"+frm+" .template").clone().removeClass("template");
		           row.find(".klId").attr("value",n.name);
		           row.find(".klId").attr("id",n.id);
		       	   row.find(".klName").text(n.name);
		           row.find(".klRemarks").text(n.remarks);
		           row.attr("id","ready"+i);//改变绑定好数据的行的id
		           row.attr("class","tr_nbw");
		           row.appendTo("#"+frm+" .grid");//添加到模板的容器中
		       });
			}

			function _submit(){
		    	var ids = top.document.getElementById("checkedIds").value;
		    	window.returnValue = ids;
				window.close();
		    }
		    
		    function _checkRole(obj){
			    var id = obj.id;
			    var roleName = obj.value;
		    	var thisRole = document.getElementById(id);
		    	//userid1|userid2|userid3|$roleName|&roleName2|roleName3|
		    	var checkedStrs = top.document.getElementById("checkedIds").value;
		    	if(checkedStrs == ""){
		    		if(thisRole.checked){
			    		checkedStrs = id+"|$"+roleName+"|";
			    	}
				}else{
					if(thisRole.checked){
						if(checkedStrs.indexOf(id+"|") == -1){
							var indexNum = checkedStrs.indexOf("$");
				    		var before = checkedStrs.substring(0,indexNum);
				    		var behind = checkedStrs.substring(indexNum);
				    		checkedStrs = before + id +"|"+ behind + roleName + "|";
						}
			    	}else{
			    		if(checkedStrs.indexOf(id+"|") != -1){
				    		var indexId = checkedStrs.indexOf(id+"|");
				    		var beginId = checkedStrs.substring(0,indexId);
				    		var endId = checkedStrs.substring(indexId + id.length +1);
				    		checkedStrs = beginId + endId;
				    		
				    		var indexName = checkedStrs.indexOf(roleName+"|");
				    		var beginName = checkedStrs.substring(0,indexName);
				    		var endName = checkedStrs.substring(indexName + roleName.length +1);
				    		checkedStrs = beginName + endName;
				    	}
			    	}
				}
		    	top.document.getElementById("checkedIds").value = checkedStrs;
		    	
		    	var all = document.getElementById("selAll");
				if(!thisRole.checked) all.checked = false;
			  	else{
			    	var aa = document.getElementsByName("checkRole");
			    	for (var i=1; i<aa.length; i++)
			     		if(!aa[i].checked) return;
			    		all.checked = true;
			  	}
		    }
		    
		   
		</script>
	</head>

<body >
	<input type="hidden" id="checkedIds" name="checkedIds"/>
	<form name="frm" method="post"
		action="<%=path %>/sys/role/list?codeId=<%=codeId %>" id="frm">
		<table><tr><td height="2px"></td></tr></table>
		&nbsp;&nbsp;&nbsp;&nbsp;
				<a href="#" onclick="_submit()">确定</a>
		<center>
		<table><tr><td height="2px"></td></tr></table>
		<table border="0" cellspacing="1" cellpadding="0" class="table_nbw  niceTable grid">
			<thead class="th_nbw">
				<tr>
					<td width="40px" >
						<input type="checkbox" name="selAll" id="selAll" class="allCheck"/>
					</td>
					<td>
						角色名称
					</td>
					<td>
						备注
					</td>
				</tr>
			</thead>
			<tbody>
				<tr class="template klId">
					<td width="40px" class="idCheck">
						<input type="checkbox" onclick="_checkRole(this)" name="checkRole" class="klId" />
					</td>
					<td class="klName niceTable-left"></td>
					<td class="klRemarks niceTable-left"></td>
				</tr>
			</tbody>
			<tfoot>
				<tr>
					<td style="border-right: none">
						<img class="loading"
							src="<%=imgPath %>/large-loading.gif" width="20px"
							class="hidden" />
					</td>
					<td colspan="4" style="border-left: none" class="txtRight">
						<span class="pagediv"></span>
					</td>
				</tr>
			</tfoot>
		</table>
		</center>
	</form>
	
<script type="text/javascript">
$(function(){
	//加载两个要使用json分页的组件
    loadGird("frm", 1);
    setTimeout(initr,"1000");//
})

function initr(){
	var allRoles = document.getElementsByName("checkRole");
   	var checkedIds = top.document.getElementById("checkedIds").value;
   	if(checkedIds == "$"){
   		checkedIds = document.getElementById("checkedIds").value;
   	}
   	if(allRoles){
	   	for(var i = 0;i<allRoles.length;i++){
	   		if(checkedIds.indexOf(allRoles[i].id+"|") != -1 )
	   			allRoles[i].checked = true;
	   	}
	   	//判断全选框是否应该选中
		var all = document.getElementById("selAll");
		var flag = true;
    	var aa = document.getElementsByName("checkRole");
    	for (var i=0; i<aa.length; i++){
     		if(!aa[i].checked) {
     			flag = false;
     			break;
     		}
     	}
     	if(aa.length==0) flag = false;
    	all.checked = flag;
   	}
}
</script>
</body>
</html>